/**
 * 
 */
package com.flying.dcss.userpermissions.action;

import java.util.LinkedList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.bo.impl.DcssUserInfo;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.flying.dcss.viewmodel.impl.UserListViewModel;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.commons.digester.rss.Item;

// TODO: Auto-generated Javadoc
/**
 * The Class UserListAction.
 *
 * @author EvanHong
 */
public class UserListAction extends ActionSupport {

	/** The user service. */
	private UserService userService;

	/** The user info service. */
	private UserInfoService userInfoService;

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Sets the user info service.
	 * 
	 * @param userInfoService
	 *            the new user info service
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		List<UserListViewModel> models = new LinkedList<UserListViewModel>();

		// Get data.
		List<DcssUser> users = userService.getAllUsers();

		for (DcssUser item : users) {
			if (userInfoService.getUserInfoByUserId(item.getId()) != null) {
				models.add(new UserListViewModel(item, userInfoService
						.getUserInfoByUserId(item.getId())));
			} else {
				models.add(new UserListViewModel(item, new DcssUserInfo()));
			}
		}
		
		//Set session.
		ServletActionContext.getRequest().setAttribute(
				SessionName.UserListSession, models);

		return this.SUCCESS;
	}
}
